#!/usr/bin/env python
################################################################################
#            __  __            _   ____        _ _ ____        _              #
#           |  \/  | ___  __ _| |_| __ )  __ _| | | __ )  ___ | |_           #
#           | |\/| |/ _ \/ _` | __|  _ \ / _` | | |  _ \ / _ \| __|         #
#           | |  | |  __/ (_| | |_| |_) | (_| | | | |_) | (_) | |_          #
#           |_|  |_|\___|\__,_|\__|____/ \__,_|_|_|____/ \___/ \__|          #
#                                                                             #
#                                     by                                       #
# _______ .______       _______  _______   ______   ______   _______   _______  #
#|   ____||   _  \     |   ____||   ____| /      | /  __  \ |       \ |   ____|  #
#|  |__   |  |_)  |    |  |__   |  |__   |  ,----'|  |  |  ||  .--.  ||  |__      #
#|   __|  |      /     |   __|  |   __|  |  |     |  |  |  ||  |  |  ||   __|      #
#|  |     |  |\  \----.|  |____ |  |____ |  `----.|  `--'  ||  '--'  ||  |____      #
#|__|     | _| `._____||_______||_______| \______| \______/ |_______/ |_______|    #
#                                                                                 #
#
# Coders
#
#	Justin Giorgi (rebel_kid)
#
# Debuggers
#
#	Justin Giorgi (rebel_kid)
#	Lachlan Gilmour (chilli0)
#	David Liedle (spiderbyte)
#
################################################################################
#                                                                              #
#                               GPL Licensed                                   #
#                                                                              #
################################################################################

# Imports
import irclib
import MySQLdb as sql
import md5
import sys
import time
import random
import os.path as os
import glob
import traceback

# Global variable holder
globalvars = {}

# Asks if user is debugging
debuging = raw_input('Are you debugging? ')
if debuging == 'yes' or debuging == 'y' or debuging == 'True':
	globalvars["debug"] = True
else:
	globalvars["debug"] = False

# Reads the config file
globalvars["home"] = os.expanduser('~')
globalvars["configf"] = open(globalvars["home"] + '/.meatballbot')
globalvars["mynick"] = globalvars["configf"].readline().split("=")[1].strip('\n')
globalvars["altnick"] = globalvars["configf"].readline().split("=")[1].strip('\n')
globalvars["password"] = globalvars["configf"].readline().split("=")[1].strip('\n')
globalvars["debugdb"] = globalvars["configf"].readline().split("=")[1].strip('\n')
globalvars["host"] = globalvars["configf"].readline().split("=")[1].strip('\n')
globalvars["user"] = globalvars["configf"].readline().split("=")[1].strip('\n')
globalvars["passwd"] = globalvars["configf"].readline().split("=")[1].strip('\n')
if globalvars["debug"]:
	globalvars["db"] = globalvars["debugdb"]
else:
	globalvars["db"] = globalvars["configf"].readline().split("=")[1].strip('\n')

# Sets startup variables
globalvars["servers"] = [] # List of server connections
globalvars["identified"] = {} # Dict of identified users (server, host):id
globalvars["delays"] = {} # Dict of chanid:last time a request was made
globalvars["lastnick"] = {} # Dict of chanid:nick that made last request
globalvars["globalalways"] = {} # Dict of functions to always perform
globalvars["localsometimes"] = {} # Dict of chanid:class
globalvars["oncall"] = {} # Dict of oncall functions name:function
globalvars["privmsg"] = {} # Dict of privmsg only functions name:function
globalvars["starttime"] = time.time() # Time the bot started
globalvars["handlers"] = {} # Dict of loaded handlers event:function
globalvars["special"] = {} # Dict of special handlers event:function
globalvars["channames"] = {} # Dict of nicks in the channel (server, channel):[nick1, nick2]

# Connects to MySQL
try:
	globalvars["conn"] = sql.connect(host = globalvars["host"], user = globalvars["user"], passwd = globalvars["passwd"], db = globalvars["db"])
	globalvars["cursor"] = globalvars["conn"].cursor()
	globalvars["cursor"].execute("SET SESSION wait_timeout=99999")

except sql.Error, e:
	print "Error %d: %s" % (e.args[0], e.args[1])
	if e.args[0] == 2006:
		globalvars["conn"] = sql.connect(host = globalvars["host"], user = globalvars["user"], passwd = globalvars["passwd"], db = globalvars["db"])
		globalvars["cursor"] = globalvars["conn"].cursor()
		globalvars["cursor"].execute("SET SESSION wait_timeout=99999")

# Passes to handlers
def welcome(server, event):
	handlers('welcome', server, event)
		
# Passes to handlers
def yourhost(server, event):
	handlers('yourhost', server, event)
		
# Passes to handlers
def created(server, event):
	handlers('created', server, event)
		
# Passes to handlers
def myinfo(server, event):
	handlers('myinfo', server, event)
		
# Passes to handlers
def featurelist(server, event):
	handlers('featurelist', server, event)
		
# Passes to handlers
def tracelink(server, event):
	handlers('tracelink', server, event)
		
# Passes to handlers
def traceconnecting(server, event):
	handlers('traceconnecting', server, event)
		
# Passes to handlers
def tracehandshake(server, event):
	handlers('tracehandshake', server, event)
		
# Passes to handlers
def traceunknown(server, event):
	handlers('traceunknown', server, event)
		
# Passes to handlers
def traceoperator(server, event):
	handlers('traceoperator', server, event)
		
# Passes to handlers
def traceuser(server, event):
	handlers('traceuser', server, event)
		
# Passes to handlers
def traceserver(server, event):
	handlers('traceserver', server, event)
		
# Passes to handlers
def traceservice(server, event):
	handlers('traceservice', server, event)
		
# Passes to handlers
def tracenewtype(server, event):
	handlers('tracenewtype', server, event)
		
# Passes to handlers
def traceclass(server, event):
	handlers('traceclass', server, event)
		
# Passes to handlers
def tracereconnect(server, event):
	handlers('tracereconnect', server, event)
		
# Passes to handlers
def statslinkinfo(server, event):
	handlers('statslinkinfo', server, event)
		
# Passes to handlers
def statscline(server, event):
	handlers('statscline', server, event)
		
# Passes to handlers
def statsnline(server, event):
	handlers('statsnline', server, event)
		
# Passes to handlers
def statsiline(server, event):
	handlers('statsiline', server, event)
		
# Passes to handlers
def statskline(server, event):
	handlers('statskline', server, event)
		
# Passes to handlers
def statsqline(server, event):
	handlers('statsqline', server, event)
		
# Passes to handlers
def statsyline(server, event):
	handlers('statsyline', server, event)
		
# Passes to handlers
def endofstats(server, event):
	handlers('endofstats', server, event)
		
# Passes to handlers
def umodeis(server, event):
	handlers('umodeis', server, event)
		
# Passes to handlers
def serviceinfo(server, event):
	handlers('serviceinfo', server, event)
		
# Passes to handlers
def endofservices(server, event):
	handlers('endofservices', server, event)
		
# Passes to handlers
def service(server, event):
	handlers('service', server, event)
		
# Passes to handlers
def servlist(server, event):
	handlers('servlist', server, event)
		
# Passes to handlers
def servlistend(server, event):
	handlers('servlistend', server, event)
		
# Passes to handlers
def statslline(server, event):
	handlers('statslline', server, event)
		
# Passes to handlers
def statsuptime(server, event):
	handlers('statsuptime', server, event)
		
# Passes to handlers
def statsoline(server, event):
	handlers('statsoline', server, event)
		
# Passes to handlers
def statshline(server, event):
	handlers('statshline', server, event)
		
# Passes to handlers
def luserconns(server, event):
	handlers('luserconns', server, event)
		
# Passes to handlers
def luserclient(server, event):
	handlers('luserclient', server, event)
		
# Passes to handlers
def luserop(server, event):
	handlers('luserop', server, event)
		
# Passes to handlers
def luserunknown(server, event):
	handlers('luserunknown', server, event)
		
# Passes to handlers
def luserchannels(server, event):
	handlers('luserchannels', server, event)
		
# Passes to handlers
def luserme(server, event):
	handlers('luserme', server, event)
		
# Passes to handlers
def adminme(server, event):
	handlers('adminme', server, event)
		
# Passes to handlers
def adminloc1(server, event):
	handlers('adminloc1', server, event)
		
# Passes to handlers
def adminloc2(server, event):
	handlers('adminloc2', server, event)
		
# Passes to handlers
def adminemail(server, event):
	handlers('adminemail', server, event)
		
# Passes to handlers
def tracelog(server, event):
	handlers('tracelog', server, event)
		
# Passes to handlers
def endoftrace(server, event):
	handlers('endoftrace', server, event)
		
# Passes to handlers
def n_local(server, event):
	handlers('n_local', server, event)
		
# Passes to handlers
def n_global(server, event):
	handlers('n_global', server, event)
		
# Passes to handlers
def none(server, event):
	handlers('none', server, event)
		
# Passes to handlers
def away(server, event):
	handlers('away', server, event)
		
# Passes to handlers
def userhost(server, event):
	handlers('userhost', server, event)
		
# Passes to handlers
def ison(server, event):
	handlers('ison', server, event)
		
# Passes to handlers
def unaway(server, event):
	handlers('unaway', server, event)
		
# Passes to handlers
def noaway(server, event):
	handlers('noaway', server, event)
		
# Passes to handlers
def whoisuser(server, event):
	handlers('whoisuser', server, event)
		
# Passes to handlers
def whoisserver(server, event):
	handlers('whoisserver', server, event)
		
# Passes to handlers
def whoisoperator(server, event):
	handlers('whoisoperator', server, event)
		
# Passes to handlers
def whowasuser(server, event):
	handlers('whowasuser', server, event)
		
# Passes to handlers
def endofwho(server, event):
	handlers('endofwho', server, event)
		
# Passes to handlers
def whoischanop(server, event):
	handlers('whoischanop', server, event)
		
# Passes to handlers
def whoisidle(server, event):
	handlers('whoisidle', server, event)
		
# Passes to handlers
def endofwhois(server, event):
	handlers('endofwhois', server, event)
		
# Passes to handlers
def whoischannels(server, event):
	handlers('whoischannels', server, event)
		
# Passes to handlers
def liststart(server, event):
	handlers('liststart', server, event)
		
# Passes to handlers
def list_(server, event):
	handlers('list', server, event)
		
# Passes to handlers
def listend(server, event):
	handlers('listend', server, event)
		
# Passes to handlers
def channelmodeis(server, event):
	handlers('channelmodeis', server, event)
		
# Passes to handlers
def channelcreate(server, event):
	handlers('channelcreate', server, event)
		
# Passes to handlers
def notopic(server, event):
	handlers('notopic', server, event)
		
# Passes to handlers
def currenttopic(server, event):
	handlers('currenttopic', server, event)
		
# Passes to handlers
def topicinfo(server, event):
	handlers('topicinfo', server, event)
		
# Passes to handlers
def inviting(server, event):
	handlers('inviting', server, event)
		
# Passes to handlers
def summoning(server, event):
	handlers('summoning', server, event)
		
# Passes to handlers
def invitelist(server, event):
	handlers('invitelist', server, event)
		
# Passes to handlers
def endofinvitelist(server, event):
	handlers('endofinvitelist', server, event)
		
# Passes to handlers
def exceptlist(server, event):
	handlers('exceptlist', server, event)
		
# Passes to handlers
def endofexceptlist(server, event):
	handlers('endofexceptlist', server, event)
		
# Passes to handlers
def version(server, event):
	handlers('version', server, event)
		
# Passes to handlers
def whoreply(server, event):
	handlers('whoreply', server, event)
		
# Passes to handlers
def namreply(server, event):
	handlers('namreply', server, event)
		
# Passes to handlers
def killdone(server, event):
	handlers('killdone', server, event)
		
# Passes to handlers
def closing(server, event):
	handlers('closing', server, event)
		
# Passes to handlers
def closeend(server, event):
	handlers('closeend', server, event)
		
# Passes to handlers
def links(server, event):
	handlers('links', server, event)
		
# Passes to handlers
def endoflinks(server, event):
	handlers('endoflinks', server, event)
		
# Passes to handlers
def endofnames(server, event):
	handlers('endofnames', server, event)
		
# Passes to handlers
def banlist(server, event):
	handlers('banlist', server, event)
		
# Passes to handlers
def endofbanlist(server, event):
	handlers('endofbanlist', server, event)
	
# Passes to handlers
def endofwhowas(server, event):
	handlers('endofwhowas', server, event)
		
# Passes to handlers
def info(server, event):
	handlers('info', server, event)
		
# Passes to handlers
def motd(server, event):
	handlers('motd', server, event)
		
# Passes to handlers
def infostart(server, event):
	handlers('infostart', server, event)
		
# Passes to handlers
def endofinfo(server, event):
	handlers('endofinfo', server, event)
		
# Passes to handlers
def motdstart(server, event):
	handlers('motdstart', server, event)
		
# Passes to handlers
def endofmotd(server, event):
	handlers('endofmotd', server, event)
		
# Passes to handlers
def motd2(server, event):
	handlers('motd2', server, event)
		
# Passes to handlers
def youreoper(server, event):
	handlers('youreoper', server, event)
		
# Passes to handlers
def rehashing(server, event):
	handlers('rehashing', server, event)
		
# Passes to handlers
def myportis(server, event):
	handlers('myportis', server, event)
		
# Passes to handlers
def time(server, event):
	handlers('time', server, event)
		
# Passes to handlers
def userstart(server, event):
	handlers('userstart', server, event)
		
# Passes to handlers
def users(server, event):
	handlers('users', server, event)
		
# Passes to handlers
def endofusers(server, event):
	handlers('endofusers', server, event)
		
# Passes to handlers
def nousers(server, event):
	handlers('nousers', server, event)
		
# Passes to handlers
def nosuchnick(server, event):
	handlers('nosuchnick', server, event)
		
# Passes to handlers
def nosuchserver(server, event):
	handlers('nosuchserver', server, event)
		
# Passes to handlers
def cannotsendtochan(server, event):
	handlers('cannotsendtochan', server, event)
		
# Passes to handlers
def toomanychannels(server, event):
	handlers('toomanychannels', server, event)
		
# Passes to handlers
def wasnosuchnick(server, event):
	handlers('wasnosuchnick', server, event)
		
# Passes to handlers
def toomanytargets(server, event):
	handlers('toomanytargets', server, event)
		
# Passes to handlers
def noorigin(server, event):
	handlers('noorigin', server, event)
		
# Passes to handlers
def norecipient(server, event):
	handlers('norecipient', server, event)
		
# Passes to handlers
def notexttosend(server, event):
	handlers('notexttosend', server, event)
		
# Passes to handlers
def notoplevel(server, event):
	handlers('notoplevel', server, event)
		
# Passes to handlers
def wildtoplevel(server, event):
	handlers('wildtoplevel', server, event)
		
# Passes to handlers
def unknowncommand(server, event):
	handlers('unknowncommand', server, event)
		
# Passes to handlers
def nomotd(server, event):
	handlers('nomotd', server, event)
		
# Passes to handlers
def noadmininfo(server, event):
	handlers('noadmininfo', server, event)
		
# Passes to handlers
def fileerror(server, event):
	handlers('fileerror', server, event)
		
# Passes to handlers
def nonicknamegiven(server, event):
	handlers('nonicknamegiven', server, event)
		
# Passes to handlers
def erroneusnickname(server, event):
	handlers('erroneusnickname', server, event)
		
# Passes to handlers
def nicknameinuse(server, event):
	handlers('nicknameinuse', server, event)
		
# Passes to handlers
def nickcollision(server, event):
	handlers('nickcollision', server, event)
		
# Passes to handlers
def unavailresource(server, event):
	handlers('unavailresource', server, event)
		
# Passes to handlers
def usernotinchannel(server, event):
	handlers('usernotinchannel', server, event)
		
# Passes to handlers
def notonchannel(server, event):
	handlers('notonchannel', server, event)
		
# Passes to handlers
def useronchannel(server, event):
	handlers('useronchannel', server, event)
		
# Passes to handlers
def nologin(server, event):
	handlers('nologin', server, event)
		
# Passes to handlers
def summondisabled(server, event):
	handlers('summondisabled', server, event)
		
# Passes to handlers
def usersdisabled(server, event):
	handlers('usersdisabled', server, event)
		
# Passes to handlers
def notregistered(server, event):
	handlers('notregistered', server, event)
		
# Passes to handlers
def needmoreparams(server, event):
	handlers('needmoreparams', server, event)
		
# Passes to handlers
def alreadyregistered(server, event):
	handlers('alreadyregistered', server, event)
		
# Passes to handlers
def nopermforhost(server, event):
	handlers('nopermforhost', server, event)
		
# Passes to handlers
def passwdmismatch(server, event):
	handlers('passwedmismatch', server, event)
		
# Passes to handlers
def yourebannedcreep(server, event):
	handlers('yourebannedcreep', server, event)
		
# Passes to handlers
def keyset(server, event):
	handlers('keyset', server, event)
		
# Passes to handlers
def channelisfull(server, event):
	handlers('channelisfull', server, event)
		
# Passes to handlers
def unknownmode(server, event):
	handlers('unknownmode', server, event)
		
# Passes to handlers
def inviteonlychan(server, event):
	handlers('inviteonlychan', server, event)
		
# Passes to handlers
def bannedfromchan(server, event):
	handlers('bannedfromchan', server, event)
		
# Passes to handlers
def badchannelkey(server, event):
	handlers('badchannelkey', server, event)
		
# Passes to handlers
def badchanmask(server, event):
	handlers('badchanmask', server, event)
		
# Passes to handlers
def nochanmodes(server, event):
	handlers('nochanmodes', server, event)
		
# Passes to handlers
def noprivileges(server, event):
	handlers('noprivileges', server, event)
		
# Passes to handlers
def chanoprivsneeded(server, event):
	handlers('chanoprivsneeded', server, event)
		
# Passes to handlers
def cantkillserver(server, event):
	handlers('cantkillserver', server, event)
		
# Passes to handlers
def restricted(server, event):
	handlers('restricted', server, event)
		
# Passes to handlers
def uniqopprivsneeded(server, event):
	handlers('uniqopprivsneeded', server, event)
		
# Passes to handlers
def nooperhost(server, event):
	handlers('nooperhost', server, event)
		
# Passes to handlers
def noservicehost(server, event):
	handlers('noservicehost', server, event)
		
# Passes to handlers
def umodeunknownflag(server, event):
	handlers('umodeunknownflag', server, event)
		
# Passes to handlers
def usersdontmatch(server, event):
	handlers('usersdontmatch', server, event)
		
# Passes to handlers
def dcc_connect(server, event):
	handlers('dcc_connect', server, event)
		
# Passes to handlers
def dcc_disconnect(server, event):
	handlers('dcc_disconnect', server, event)
		
# Passes to handlers
def dccmsg(server, event):
	handlers('dccmsg', server, event)
		
# Passes to handlers
def ctcpreply(server, event):
	handlers('ctcpreply', server, event)
		
# Passes to handlers
def error(server, event):
	handlers('error', server, event)
		
# Passes to handlers
def ping(server, event):
	handlers('ping', server, event)
		
# Passes to handlers
def privmsg(server, event):
	handlers('privmsg', server, event)
		
# Passes to handlers
def privnotice(server, event):
	handlers('privnotice', server, event)
		
# Passes to handlers
def pubmsg(server, event):
	handlers('pubmsg', server, event)
		
# Passes to handlers
def pubnotice(server, event):
	handlers('pubnotice', server, event)
		
# Passes to handlers
def pong(server, event):
	handlers('pong', server, event)
		
# Passes to handlers
def disconnect(server, event):
	handlers('disconnect', server, event)
		
# Passes to handlers
def ctcp(server, event):
	handlers('ctcp', server, event)
		
# Passes to handlers
def join(server, event):
	handlers('join', server, event)
		
# Passes to handlers
def kick(server, event):
	handlers('kick', server, event)
		
# Passes to handlers
def mode(server, event):
	handlers('mode', server, event)
		
# Passes to handlers
def part(server, event):
	handlers('part', server, event)
		
# Passes to handlers
def quit(server, event):
	handlers('quit', server, event)
		
# Passes to handlers
def invite(server, event):
	handlers('invite', server, event)
		
# Passes to handlers
def quit(server, event):
	handlers('quit', server, event)
		
# Passes to handlers
def nick(server, event):
	handlers('nick', server, event)
		
# Passes to handlers
def tryagain(server, event):
	handlers('tryagain', server, event)
	
def handlers(name, server, event):
	
	# Calls the variable holder
	global globalvars
	
	# Handler try
	try:
	
		# Checks if a module has a handler for the event
		if name in globalvars["handlers"].keys():
			globalvars["handlers"][name](globalvars, server, event)
	
		# Checks if a special handler was created for the event	
		elif (server, name) in globalvars["special"].keys():
			globalvars["special"][(server, name)].reply(globalvars, server, event)
			del globalvars["special"][(server, name)]
			
	# Prints traceback on error
	except:
		traceback.print_exc()

# Startup loads		
def load(globalvars):

	# Gets modules to load
	globalvars["cursor"].execute("select module from modules where import = 1")
	data = globalvars["cursor"].fetchall()
	
	# Loads modules
	try:
		if data is not None:
			for row in data:
				mymod = __import__(row[0])
				mymod.autoload(globalvars)
	except:
		traceback.print_exc()

# Startup loads
load(globalvars)

# IRC Connection
port = 6667
irc = irclib.IRC()

# Add event handlers
irc.add_global_handler('welcome', welcome)
irc.add_global_handler('yourhost', yourhost)
irc.add_global_handler('created', created)
irc.add_global_handler('myinfo', myinfo)
irc.add_global_handler('featurelist', featurelist)
irc.add_global_handler('tracelink', tracelink)
irc.add_global_handler('traceconnecting', traceconnecting)
irc.add_global_handler('tracehandshake', tracehandshake)
irc.add_global_handler('traceunknown', traceunknown)
irc.add_global_handler('traceoperator', traceoperator)
irc.add_global_handler('traceuser', traceuser)
irc.add_global_handler('traceserver', traceserver)
irc.add_global_handler('traceservice', traceservice)
irc.add_global_handler('tracenewtype', tracenewtype)
irc.add_global_handler('traceclass', traceclass)
irc.add_global_handler('tracereconnect', tracereconnect)
irc.add_global_handler('statslinkinfo', statslinkinfo)
irc.add_global_handler('statscline', statscline)
irc.add_global_handler('statsnline', statsnline)
irc.add_global_handler('statsiline', statsiline)
irc.add_global_handler('statskline', statskline)
irc.add_global_handler('statsqline', statsqline)
irc.add_global_handler('statsyline', statsyline)
irc.add_global_handler('endofstats', endofstats)
irc.add_global_handler('umodeis', umodeis)
irc.add_global_handler('serviceinfo', serviceinfo)
irc.add_global_handler('endofservices', endofservices)
irc.add_global_handler('service', service)
irc.add_global_handler('servlist', servlist)
irc.add_global_handler('servlistend', servlistend)
irc.add_global_handler('statslline', statslline)
irc.add_global_handler('statsuptime', statsuptime)
irc.add_global_handler('statsoline', statsoline)
irc.add_global_handler('statshline', statshline)
irc.add_global_handler('luserconns', luserconns)
irc.add_global_handler('luserclient', luserclient)
irc.add_global_handler('luserop', luserop)
irc.add_global_handler('luserunknown', luserunknown)
irc.add_global_handler('luserchannels', luserchannels)
irc.add_global_handler('luserme', luserme)
irc.add_global_handler('adminme', adminme)
irc.add_global_handler('adminloc1', adminloc1)
irc.add_global_handler('adminloc2', adminloc2)
irc.add_global_handler('adminemail', adminemail)
irc.add_global_handler('tracelog', tracelog)
irc.add_global_handler('endoftrace', endoftrace)
irc.add_global_handler('tryagain', tryagain)
irc.add_global_handler('n_local', n_local)
irc.add_global_handler('n_global', n_global)
irc.add_global_handler('none', none)
irc.add_global_handler('away', away)
irc.add_global_handler('userhost', userhost)
irc.add_global_handler('ison', ison)
irc.add_global_handler('unaway', unaway)
irc.add_global_handler('noaway', noaway)
irc.add_global_handler('whoisuser', whoisuser)
irc.add_global_handler('whoisserver', whoisserver)
irc.add_global_handler('whoisoperator', whoisoperator)
irc.add_global_handler('whowasuser', whowasuser)
irc.add_global_handler('endofwho', endofwho)
irc.add_global_handler('whoischanop', whoischanop)
irc.add_global_handler('whoisidle', whoisidle)
irc.add_global_handler('endofwhois', endofwhois)
irc.add_global_handler('whoischannels', whoischannels)
irc.add_global_handler('liststart', liststart)
irc.add_global_handler('list', list_)
irc.add_global_handler('listend', listend)
irc.add_global_handler('channelmodeis', channelmodeis)
irc.add_global_handler('channelcreate', channelcreate)
irc.add_global_handler('notopic', notopic)
irc.add_global_handler('currenttopic', currenttopic)
irc.add_global_handler('topicinfo', topicinfo)
irc.add_global_handler('inviting', inviting)
irc.add_global_handler('summoning', summoning)
irc.add_global_handler('invitelist', invitelist)
irc.add_global_handler('endofinvitelist', endofinvitelist)
irc.add_global_handler('exceptlist', exceptlist)
irc.add_global_handler('endofexceptlist', endofexceptlist)
irc.add_global_handler('version', version)
irc.add_global_handler('whoreply', whoreply)
irc.add_global_handler('namreply', namreply)
irc.add_global_handler('killdone', killdone)
irc.add_global_handler('closing', closing)
irc.add_global_handler('closeend', closeend)
irc.add_global_handler('links', links)
irc.add_global_handler('endoflinks', endoflinks)
irc.add_global_handler('endofnames', endofnames)
irc.add_global_handler('banlist', banlist)
irc.add_global_handler('endofbanlist', endofbanlist)
irc.add_global_handler('endofwhowas', endofwhowas)
irc.add_global_handler('info', info)
irc.add_global_handler('motd', motd)
irc.add_global_handler('infostart', infostart)
irc.add_global_handler('endofinfo', endofinfo)
irc.add_global_handler('motdstart', motdstart)
irc.add_global_handler('endofmotd', endofmotd)
irc.add_global_handler('motd2', motd2)
irc.add_global_handler('youreoper', youreoper)
irc.add_global_handler('rehashing', rehashing)
irc.add_global_handler('myportis', myportis)
irc.add_global_handler('time', time)
irc.add_global_handler('userstart', userstart)
irc.add_global_handler('users', users)
irc.add_global_handler('endofusers', endofusers)
irc.add_global_handler('nousers', nousers)
irc.add_global_handler('nosuchnick', nosuchnick)
irc.add_global_handler('nosuchserver', nosuchserver)
irc.add_global_handler('cannotsendtochan', cannotsendtochan)
irc.add_global_handler('toomanychannels', toomanychannels)
irc.add_global_handler('wasnosuchnick', wasnosuchnick)
irc.add_global_handler('toomanytargets', toomanytargets)
irc.add_global_handler('noorigin', noorigin)
irc.add_global_handler('norecipient', norecipient)
irc.add_global_handler('notexttosend', notexttosend)
irc.add_global_handler('notoplevel', notoplevel)
irc.add_global_handler('wildtoplevel', wildtoplevel)
irc.add_global_handler('unknowncommand', unknowncommand)
irc.add_global_handler('nomotd', nomotd)
irc.add_global_handler('noadmininfo', noadmininfo)
irc.add_global_handler('fileerror', fileerror)
irc.add_global_handler('nonicknamegiven', nonicknamegiven)
irc.add_global_handler('erroneusnickname', erroneusnickname)
irc.add_global_handler('nicknameinuse', nicknameinuse)
irc.add_global_handler('nickcollision', nickcollision)
irc.add_global_handler('unavailresource', unavailresource)
irc.add_global_handler('usernotinchannel', usernotinchannel)
irc.add_global_handler('notonchannel', notonchannel)
irc.add_global_handler('useronchannel', useronchannel)
irc.add_global_handler('nologin', nologin)
irc.add_global_handler('summondisabled', summondisabled)
irc.add_global_handler('usersdisabled', usersdisabled)
irc.add_global_handler('notregistered', notregistered)
irc.add_global_handler('needmoreparams', needmoreparams)
irc.add_global_handler('alreadyregistered', alreadyregistered)
irc.add_global_handler('nopermforhost', nopermforhost)
irc.add_global_handler('passwdmismatch', passwdmismatch)
irc.add_global_handler('yourebannedcreep', yourebannedcreep)
irc.add_global_handler('keyset', keyset)
irc.add_global_handler('channelisfull', channelisfull)
irc.add_global_handler('unknownmode', unknownmode)
irc.add_global_handler('inviteonlychan', inviteonlychan)
irc.add_global_handler('bannedfromchan', bannedfromchan)
irc.add_global_handler('badchannelkey', badchannelkey)
irc.add_global_handler('badchanmask', badchanmask)
irc.add_global_handler('nochanmodes', nochanmodes)
irc.add_global_handler('noprivileges', noprivileges)
irc.add_global_handler('chanoprivsneeded', chanoprivsneeded)
irc.add_global_handler('cantkillserver', cantkillserver)
irc.add_global_handler('restricted', restricted)
irc.add_global_handler('uniqopprivsneeded', uniqopprivsneeded)
irc.add_global_handler('nooperhost', nooperhost)
irc.add_global_handler('noservicehost', noservicehost)
irc.add_global_handler('umodeunknownflag', umodeunknownflag)
irc.add_global_handler('usersdontmatch', usersdontmatch)
irc.add_global_handler('dcc_connect', dcc_connect)
irc.add_global_handler('dcc_disconnect', dcc_disconnect)
irc.add_global_handler('dccmsg', dccmsg)
irc.add_global_handler('ctcpreply', ctcpreply)
irc.add_global_handler('error', error)
irc.add_global_handler('ping', ping)
irc.add_global_handler('privmsg', privmsg)
irc.add_global_handler('privnotice', privnotice)
irc.add_global_handler('pubmsg', pubmsg)
irc.add_global_handler('pubnotice', pubnotice)
irc.add_global_handler('pong', pong)
irc.add_global_handler('disconnect', disconnect)
irc.add_global_handler('ctcp', ctcp)
irc.add_global_handler('join', join)
irc.add_global_handler('kick', kick)
irc.add_global_handler('mode', mode)
irc.add_global_handler('part', part)
irc.add_global_handler('quit', quit)
irc.add_global_handler('invite', invite)
irc.add_global_handler('quit', quit)
irc.add_global_handler('nick', nick)

# Initial Connect
globalvars["connect"](globalvars)

# Begin forever loop
irc.process_forever()
